<template>
  <div id="app">
    <transition :name="transitionName">
      <router-view class="router-view" />
    </transition>
  </div>
</template> 
 
<script>
// 防止移动端点击没用
import fastclick from "fastclick";

export default {
  data() { 
    return {  
      transitionName: 'slide-left'
    }
  },
  // 监听路由的路径，可以通过不同的路径去选择不同的切换效果
  watch: {
    $route(to, from) {
      if (to.meta.index > from.meta.index) {//一级路由向二级路由切换，向左滑动
        this.transitionName = 'slide-left'
      } else if (to.meta.index < from.meta.index) {//二级路由向一级路由切换，向右滑动
        this.transitionName = 'slide-right'
      } else {//同级路由切换 无过渡效果
        this.transitionName = ''
      }
    }
  },
  mounted() {
    //解决移动端点击300ms延时
    fastclick.attach(document.body);
  }
}
</script>

<style lang="scss">
// 路由切换过滤效果
#app {
  width: 100%;
  max-width: 800px;
  height: 100%;
  margin: 0 auto;
  background: #fff;
  -webkit-overflow-scrolling: touch;
  position: absolute;
  left: 0;
  top: 0;
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

/*切换样式出错处理*/
.router-view {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  margin: 0 auto;
  -webkit-overflow-scrolling: touch;
}

/*切换动画*/
.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
  height: 100%;
  will-change: transform;
  transition: all 500ms;
  position: absolute;
  backface-visibility: hidden;
}

.slide-right-enter {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}

.slide-right-leave-active {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}

.slide-left-enter {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}

.slide-left-leave-active {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}
</style>
